
import os
import pandas as pd

def get_datas(data_dir='./Data'):
    user_profile_fn = 'user_profile_table.csv'
    user_balance_fn = 'user_balance_table.csv'
    bank_shibor_fn = 'mfd_bank_shibor.csv'
    share_interest_fn = 'mfd_day_share_interest.csv'

    user_profile_df = pd.read_csv(os.path.join(data_dir,user_profile_fn))
    user_balance_df = pd.read_csv(os.path.join(data_dir,user_balance_fn))

    return user_profile_df,user_balance_df

def flit_notzero(user_balance_df):
    user_balance_df_nozero = user_balance_df[user_balance_df['total_purchase_amt'] + user_balance_df['total_redeem_amt'] > 0]
    return user_balance_df_nozero

def parse_to_balancebydate(user_balance_df:pd.DataFrame):
    user_balance_df['datetime'] = pd.to_datetime(user_balance_df['report_date'], format= "%Y%m%d")
    balanceGroupbyDate = user_balance_df.groupby(['datetime'],as_index=True)
    """
    所需数据：
        每天的存入总量：total_purchase_amt
        每天的取出总量：total_redeem_amt
        每天出现活动的用户数量：user_count
        每天的日期 以及 年月日周数

    """

    balance_bydate_df = balanceGroupbyDate.agg({
        'tBalance':'sum',
        'yBalance':'sum',
        'share_amt':'sum',
        'total_purchase_amt':'sum',
        'total_redeem_amt':'sum',
        'user_id':'count',
    })
    balance_bydate_df['datetime'] = balance_bydate_df.index
    balance_bydate_df['day'] = balance_bydate_df['datetime'].dt.day
    balance_bydate_df['month'] = balance_bydate_df['datetime'].dt.month
    balance_bydate_df['year'] = balance_bydate_df['datetime'].dt.year
    balance_bydate_df['week'] = balance_bydate_df['datetime'].dt.isocalendar().week
    balance_bydate_df['weekday'] = balance_bydate_df['datetime'].dt.weekday
    balance_bydate_df.rename(columns={'user_id':'user_count'},inplace = True)

    return balance_bydate_df,balanceGroupbyDate


def parse_to_balancebydate_detail(user_balance_df:pd.DataFrame):
    user_balance_df['datetime'] = pd.to_datetime(user_balance_df['report_date'], format= "%Y%m%d")
    balanceGroupbyDate = user_balance_df.groupby(['datetime'],as_index=True)
    """
    所需数据：
        每天的存入总量：total_purchase_amt
        每天的取出总量：total_redeem_amt
        每天出现活动的用户数量：user_count
        每天的日期 以及 年月日周数

        输入尽可能多的每天特征： 每天所有数值总和，平均、中位，差分，用户数量相关（group表）和两个利率表 每天第几月、一月第几天、第几周、一周星期几、是否为假日（距离关系可自动挖掘）

    """

    user_nums_infoDF = get_user_num_infos(balanceGroupbyDate)

    # def get_everydetail_fromOneDateDF(oneDateDF:pd.DataFrame):
    #     resDF = pd.DataFrame()
    #     resDF['user_count'] = oneDateDF['user_id'].count() # 当天出现了多少用户的活动
    #     resDF



    balance_bydate_df = balanceGroupbyDate.agg({
        'total_purchase_amt':'sum',
        'total_redeem_amt':'sum',
        'tBalance':'sum',
        'yBalance':'sum',
        'share_amt':'sum',
        'user_id':'count',
        'direct_purchase_amt':'sum',
        'purchase_bal_amt':'sum',
        'purchase_bank_amt':'sum',
        'total_redeem_amt':'sum',
        'consume_amt':'sum',
        'transfer_amt':'sum',
        'tftobal_amt':'sum',
        'tftocard_amt':'sum',
        'share_amt':'sum',
        'category1':'sum',
        'category2':'sum',
        'category3':'sum',
        'category4':'sum',
    })
    balance_bydate_df['datetime'] = balance_bydate_df.index
    balance_bydate_df['day'] = balance_bydate_df['datetime'].dt.day
    balance_bydate_df['month'] = balance_bydate_df['datetime'].dt.month
    balance_bydate_df['year'] = balance_bydate_df['datetime'].dt.year
    balance_bydate_df['week'] = balance_bydate_df['datetime'].dt.isocalendar().week
    balance_bydate_df['weekday'] = balance_bydate_df['datetime'].dt.weekday
    balance_bydate_df.rename(columns={'user_id':'user_count'},inplace = True)

    balance_bydate_df = pd.merge(balance_bydate_df,user_nums_infoDF,left_index=True,right_index=True)

    return balance_bydate_df,balanceGroupbyDate

def getUserInfo_fromOneDateDF(oneDateDF:pd.DataFrame):
    userInfoDF = pd.DataFrame()
    userInfoDF['user_count'] = oneDateDF['user_id'].count() # 当天出现了多少用户的活动

def get_user_num_infos(balanceGroupbyDate):
    each_datetimes = list()
    each_userid_sets: list[set] = list()

    def watch_user_bydate(one_date_df: pd.DataFrame):
        each_datetimes.append(one_date_df['datetime'].iloc[0])
        each_userid_sets.append(set(one_date_df['user_id']))

    balanceGroupbyDate.apply(watch_user_bydate)
    counts_dict = {
        '累计活动用户数量': list(),
        '当天活动用户数量': list(),
        '当天新增用户数量': list(),
    }

    all_user_set = set()
    for oneday_userid_set in each_userid_sets:
        counts_dict['当天活动用户数量'].append(len(oneday_userid_set))
        curday_newuserids = oneday_userid_set.difference(all_user_set)
        counts_dict['当天新增用户数量'].append(len(curday_newuserids))
        all_user_set.update(oneday_userid_set)
        counts_dict['累计活动用户数量'].append(len(all_user_set))

    counts_dict['当天丢失用户数量'] = list()
    all_user_set_rev = set()
    all_user_set_rev.update(each_userid_sets[-1])
    for i in range(len(each_userid_sets)-1, -1, -1):
        oneday_userid_set = each_userid_sets[i]

        curday_lostnewuserids = oneday_userid_set.difference(all_user_set_rev)
        counts_dict['当天丢失用户数量'].append(-len(curday_lostnewuserids))

        all_user_set_rev.update(oneday_userid_set)

    return pd.DataFrame(counts_dict,index=each_datetimes)